<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 系统用户组管理
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class AdminGroup extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}		
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());		
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = "admin_group";
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['disabled'] = $this->input->post('disabled');
			$data['parentId'] = $this->input->post('parentId');
			$data['title'] = $this->input->post('title');
			$data['createTimeStart'] = $this->input->post('createTimeStart');
			$data['createTimeEnd'] = $this->input->post('createTimeEnd');
		}else{
			$data['disabled'] = $this->input->get('disabled');
			$data['parentId'] = $this->input->get('parentId');
			$data['title'] = $this->input->get('title');
			$data['createTimeStart'] = $this->input->get('createTimeStart');
			$data['createTimeEnd'] = $this->input->get('createTimeEnd');
		}
		$data['title'] = urldecode($data['title']);

		$condition = array();
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['disabled']>0)){
			$condition['disabled'] = $data['disabled'];
		}
		if(strlen($data['parentId'])>0){
			array_push($db_like_key,'parentId');
			array_push($db_like_value,strtoupper($data['parentId']));
		}
		if(strlen($data['title'])>0){
			array_push($db_like_key,'title');
			array_push($db_like_value,$data['title']);
		}
		if(strlen($data['createTimeStart'])>0){
			$condition['createTime >= '] = $data['createTimeStart'];
		}
		if(strlen($data['createTimeEnd'])>0){
			$condition['createTime <= '] = date('Y-m-d',strtotime($data['createTimeEnd']) + 24*60*60);
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}

		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;

		if($this->input->server('REQUEST_METHOD')=='POST'){
			switch(strtoupper($active)){
				case 'DEL':
					//删除
					if(strlen($del)==0){
						//删除所选
						$condition['db_where_in'] = array('id',$id);
					}
					$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$condition);
					break;
				case 'REMOVE':
					//移动
					$condition2['db_where_in'] = array('id',$id);		
					$parentId = $this->input->post('parentId');
					break;
				case 'DISABLED':
					//启用状态
					$condition2['db_where_in'] = array('id',$id);
					$disabled = $this->input->post('disabled');
					$this->JayphaDB->updateDB(array('disabled'=>$disabled),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'NEWEST':
					//最新状态
					$condition2['db_where_in'] = array('id',$id);
					$newest = $this->input->post('newest');
					$this->JayphaDB->updateDB(array('newest'=>$newest),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'TOP':
					//置顶状态
					$condition2['db_where_in'] = array('id',$id);
					$top = $this->input->post('top');
					$this->JayphaDB->updateDB(array('top'=>$top),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'RECOMMEND':
					//推荐状态
					$condition2['db_where_in'] = array('id',$id);
					$recommend = $this->input->post('recommend');
					$this->JayphaDB->updateDB(array('recommend'=>$recommend),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'SHOWHOME':
					//首页显示状态
					$condition2['db_where_in'] = array('id',$id);
					$showhome = $this->input->post('showhome');
					$this->JayphaDB->updateDB(array('showhome'=>$showhome),$this->dbTablePrefix.$tvName,$condition2);
					break;
			}
		}

		unset($condition);

		$config['base_url'] = site_url("admin/adminGroup/index");
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		//$config['full_tag_open'] = '<div>';
		//$config['full_tag_close'] = '</div>';
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';

		$this->pagination->initialize($config);

		$data['currentSite'] = '常规管理<span class="split">>></span>系统用户组管理<span class="split">>></span>系统用户组列表';
		$adminGroupArr = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.$tvName,'','parentId,id',false));
		$data['adminGroupAll'] = $this->JayphaDB->getChildrenList($adminGroupArr,0,1);
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = '系统用户组管理';
		$data['editPage'] = site_url('admin/adminGroup/edit');
		$data['editTitle'] = '修改系统用户组';
		$data['addTitle'] = '添加系统用户组';
		$data['perm'] = 'adminGroup';
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'parentId,id';
		$num = $this->uri->segment(4,0);
		$data['adminGroupList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		foreach($data['adminGroupList'] as $adminGroup){
			if($adminGroup->parentId!=0){
				$adminGroup->parentTitle = $this->JayphaDB->getDBValue('title',$this->dbTablePrefix.$tvName,array('id'=>$adminGroup->parentId),'');
			}else{
				$adminGroup->parentTitle = '顶级分类';
			}
		}
		$this->load->view("admin/$this->adminCurrentTemplate/adminGroup",$data);
	}
	
	function edit(){
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'admin_group';
		$data['tvName'] = $tvName;
		$data['perm'] = 'adminGroup';
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$data['id'] = $this->input->post('id');
			$data['parentId'] = $this->input->post('parentId');
			$data['disabled'] = $this->input->post('disabled');
			$data['title'] = $this->input->post('title');
			$data['permission'] = $this->input->post('permissions');
			if(strlen($data['title'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>280,
						'height'=>108,
						'title'=>'名称错误',
						'content'=>'<span class="red">对不起，名称不能为空，请输入名称！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if(strlen($data['id'])==0){
				//添加
				$insertData = array(
					'parentId'=>$data['parentId'],
					'disabled'=>$data['disabled'],
					'title'=>$data['title'],
					'permission'=>$data['permission']
				);
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('upper(title)'=>strtoupper($data['title'])))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>320,
							'height'=>108,
							'title'=>'添加失败',
							'content'=>'<span class="red">对不起，该名称已经存在，请选择其它名称！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				
				$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$insertData,true);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>240,
						'height'=>108,
						'title'=>'添加成功 ',
						'content'=>'您已成功添加一组管理员组！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
				
			}else{
				//修改
				$where = array('id'=>$data['id']);
				$updateData = array(
					'parentId'=>$data['parentId'],
					'disabled'=>$data['disabled'],
					'title'=>$data['title'],
					'permission'=>$data['permission']
				);
				
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$data['id'],'upper(title)'=>strtoupper($data['title'])))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>320,
							'height'=>108,
							'title'=>'修改失败',
							'content'=>'<span class="red">对不起，该名称已经存在，请选择其它名称！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				
				$this->JayphaDB->updateDB($updateData,$this->dbTablePrefix.$tvName,$where);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>220,
						'height'=>108,
						'title'=>'修改成功 ',
						'content'=>'您的管理员组已成功修改！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}	
		}else{
			$data['id'] = $this->uri->segment(4);
			if(strlen($data['id'])==0){
				$data['parentId'] = 0;
				$data['disabled'] = 0;
				$data['title'] = '';
				$data['permission'] = '';
			}else{
				$adminGroup = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'',false);
				if(count($adminGroup)!=0){
					foreach($adminGroup as $row){
						$data['disabled'] = $row->disabled;
						$data['parentId'] = $row->parentId;
						$data['title'] = $row->title;
						$data['permission'] = $row->permission;
					}
				}else{
					$data['disabled'] = 0;
					$data['parentId'] = 0;
					$data['title'] = '';
					$data['permission'] = '';
				}
			}
		}
		$adminGroupAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.$tvName,array('disabled'=>0),'parentId,id',false));
		$data['adminGroupAll'] = $this->JayphaDB->getChildrenList($adminGroupAll,0,1);
		$this->load->view("admin/$this->adminCurrentTemplate/adminGroupEdit",$data);
	}
}

/* End of file adminGroup.php */
/* Location: ./application/controllers/admin/adminGroup.php */